package fiducialTuioAS3 
{
	/**
	 * @class TestFiducialHandler.as
	 * @namespace 
	 * @author Frederic Friess and Johannes Luderschmidt (http://johannesluderschmidt.de/tuio-actionscript-3-open-source-fiducial-object-interaction-implementation/552/)
	 * NOTE: Creative Commons Attribution 3.0 License (http://creativecommons.org/licenses/by/3.0/)
	 * You are free: to Share — to copy, distribute and transmit the work
	 * You are free: to Remix — to adapt the work
	 * Under the following conditions: 
	 * Attribution. You must attribute the work in the manner specified by the author or licensor 
	 * (but not in any way that suggests that they endorse you or your use of the work).
	 * 
	 * Feel free to download and modify the code for any use as long as our name is in there somewhere.
	 * You may also use it in comercial projects without any licensing fees. 
	 */
	import CustomEvent.CustomEvent;

	import fiducialTuioAS3.tuio.PropEvent;
	import fiducialTuioAS3.tuio.TUIOPropInterface;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;

	/**
	 * TestFiducialHandler shows how a Handler for fiducial events can be implemented in this
	 * framework.
	 * 
	 * The handler must implement the interface TUIOPropInterface in order to work with TuioManager
	 * and PropEvents.
	 * 
	 * This example will just draw a bigger circle at the position of the Tuio object. Another little 
	 * circle will be added on top of the bigger circle in order to show the rotation a Tuio object.
	 * 
	 */
	public class KeywordFiducialHandler extends Sprite implements TUIOPropInterface
	{
		
////////////////////////////////////////////////////////////////////////////////
/////// VARIABLES //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
		
		/*
		 * BOUNCE_THRESHOLD contains the minimum distance that a fiducial must be
		 * moved on the table in order that the object is being moved in this handler.
		 * This is necessary because reacTIVision is constantly sending move events,
		 * which causes the graphics on the table to shiver. This is not necessary in
		 * the simulator. However, for completeness it is included here.
		 */
		private var BOUNCE_THRESHOLD_MOVE:Number = 10;
		/*
		 * rotation in Flash starts at -90 degrees. The simulator starts at 0
		 * degrees. This variable represents that difference.
		 */
		private var lastXVal:Number = 0;
		private var lastYVal:Number = 0;
		
		private var _id : int;
		
		/*
		 * CustomEvents an die GUI zur Verwaltung der Länder und Keywords
		 */
		public static const KEYWORD_ADDED : String = "keywordAdded";
		public static const KEYWORD_MOVED : String = "keywordMoved";
		public static const KEYWORD_ROTATED : String = "keywordRotated";
		public static const KEYWORD_REMOVED : String = "keywordRemoved";


////////////////////////////////////////////////////////////////////////////////
/////// CONSTRUCTOR ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
		public function KeywordFiducialHandler(p_id : int)
		{
//			trace("KeywordfiducialHandler -> Constructor init...");
			_id = p_id;
		}

		
		
		
		
////////////////////////////////////////////////////////////////////////////////
/////// METHODS ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
		

/////// Errors /////////////////////////////////////////////////////////////////
		private function ioError(e:IOErrorEvent)
		{
//			trace(this, "error while loading: " + e.target);
		}
		
		
		
		
		/**
		 * will be executed everytime a fiducial to which this handler is applied is being
		 * added on the table/simulator. (This is a method from TUIOPropInterface that must 
		 * be implemented in a Tuio object handler.)
		 */
/////// onAdd //////////////////////////////////////////////////////////////////	
		public function onAdd(evt:PropEvent)
		{
//			trace("KeywordFiducialHandler -> onAdd() init ...");
			dispatchEvent(new CustomEvent(KEYWORD_ADDED, evt));
		}
	
	
	
	
		/**
		 * will be executed everytime a fiducial to which this handler is applied is being
		 * removed from the table/simulator.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
/////// onRemove ///////////////////////////////////////////////////////////////		
		public function onRemove(evt:PropEvent) 
		{
//			trace("KeywordFiducialHandler -> onRemove() ...");
			
			try{
				// nothing to remove here
			}catch(error:Error){
//				trace(this, "error when trying to remove:", error.message);
			}
			/*
			 * Der GUI sagen, dass das Keyword deaktiviert ist
			 */
			dispatchEvent(new CustomEvent(KEYWORD_REMOVED, evt));
		}
		
		
		
	
		
		/**
		 * will be executed everytime a fiducial to which this handler is applied is being
		 * moved on the table / in the simulator.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
/////// onMove /////////////////////////////////////////////////////////////////		
		public function onMove(evt:PropEvent)  
		{
			/* 
			 *  prevent shivering of object on the actual table:
			 *  object may only be moved if a certain movement 
			 *  threshold has been exceeded (try without on a mt
			 *  table and you'll see)
			 */
			var distance:Number = Math.sqrt((int(evt.xpos) - lastXVal)*(int(evt.xpos) - lastXVal) +  (int(evt.ypos) - lastYVal) * (int(evt.ypos) - lastYVal));
			
			if(distance > BOUNCE_THRESHOLD_MOVE)
			{
				dispatchEvent(new CustomEvent(KEYWORD_MOVED, evt));
				/*
				 * Die eingerasteten X/Y-Koordinaten speichern
				 */
				lastXVal = int(evt.xpos);
				lastYVal = int(evt.ypos);
			}
			
		}
		
		
		
		
		
		/*
		 * will be executed everytime a fiducial to which this handler is applied is being
		 * rotated on the table / in the simulator.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
/////// onRotate ///////////////////////////////////////////////////////////////		
		public function onRotate(evt:PropEvent) 
		{
//			trace("KeywordFiducialHandler -> onRotate() ...");
			dispatchEvent(new CustomEvent(KEYWORD_ROTATED, evt));
		}
		
		
		
		
		
		
		
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * standard Tuio property that is not used in this handler.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
		public function onMoveVelocety(evt:PropEvent){}
		
		/**
		 * standard Tuio property that is not used in this handler.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
		public function onRotateVelocety(evt:PropEvent){}
		
		/**
		 * standard Tuio property that is not used in this handler.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
		public function onMoveAccel(evt:PropEvent){}
		
		/**
		 * standard Tuio property that is not used in this handler.   
		 * (This is a method from TUIOPropInterface that must be implemented in a Tuio object 
		 * handler.)
		 */
		public function onRotateAccel(evt:PropEvent){}


		
////////////////////////////////////////////////////////////////////////////////		
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////		
	
		
	}
}